package com.dream.root.eureka.eventListener;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.shared.Applications;
import com.netflix.eureka.EurekaServerContextHolder;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * 监听事件
 * User: liulinhui
 * Date: 27/07/2018
 * Time: 12:49
 * Description: EurekaInstanceEventListener
 */
@EnableScheduling
@Slf4j
@Component
public class EurekaInstanceEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {

        //服务down掉了
        if (applicationEvent instanceof EurekaInstanceCanceledEvent) {
            EurekaInstanceCanceledEvent event = (EurekaInstanceCanceledEvent) applicationEvent;
            PeerAwareInstanceRegistry registry = EurekaServerContextHolder.getInstance().getServerContext().getRegistry();
            Applications applications = registry.getApplications();
            applications.getRegisteredApplications().forEach(registeredApplication -> {
                registeredApplication.getInstances().forEach(instanceInfo -> {
                    if (instanceInfo.getInstanceId().equals(event.getServerId())) {
                        log.error("服务" + instanceInfo.getAppName() + "(" + instanceInfo.getIPAddr() + ":" + instanceInfo.getPort() + ")挂掉啦");
                        //todo 消息通知
                    }
                });
            });
        }

        //服务注册成功
        if (applicationEvent instanceof EurekaInstanceRegisteredEvent) {
            EurekaInstanceRegisteredEvent event = (EurekaInstanceRegisteredEvent) applicationEvent;
            InstanceInfo instanceInfo = event.getInstanceInfo();
            log.info("服务" + instanceInfo.getAppName() + "(" + instanceInfo.getIPAddr() + ":" + instanceInfo.getPort() + ")注册成功了！");
        }

        //服务续约
        if (applicationEvent instanceof EurekaInstanceRenewedEvent) {
            InstanceInfo instanceInfo = ((EurekaInstanceRenewedEvent) applicationEvent).getInstanceInfo();
            log.trace("服务" + instanceInfo.getAppName() + "(" + instanceInfo.getIPAddr() + ":" + instanceInfo.getPort() + ")续约成功！");
        }

        //服务可用
        if (applicationEvent instanceof EurekaRegistryAvailableEvent) {
            log.info("服务 available");
        }

        //启动服务
        if (applicationEvent instanceof EurekaServerStartedEvent) {
            log.info("服务 started");
        }
    }
}

